package com.jf.blog_jf04.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jf.blog_jf04.bean.Blog;
import com.jf.blog_jf04.bean.Comment;
import com.jf.blog_jf04.data.PageInfo;
import com.jf.blog_jf04.data.dto.CommentDto;
import com.jf.blog_jf04.data.ov.ApiCommentOV;
import com.jf.blog_jf04.data.ov.CommentSearchOV;
import com.jf.blog_jf04.mapper.BlogMapper;
import com.jf.blog_jf04.mapper.CommentMapper;
import com.jf.blog_jf04.service.CommentService;
import com.jf.blog_jf04.utils.BeanConvertUtils;
import com.jf.blog_jf04.utils.MyConstants;
import com.jf.blog_jf04.utils.R;
import com.jf.blog_jf04.utils.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @author pang6
 * @description 针对表【comment(评论表)】的数据库操作Service实现
 * @createDate 2025-10-18 14:32:11
 */
@Service
@Transactional
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment>
        implements CommentService {

    @Autowired
    private BlogMapper blogMapper;

    @Override
    public R getPage(PageInfo pageInfo, CommentSearchOV form) {
        QueryWrapper<Comment> qw = new QueryWrapper<>();
        qw.eq("blog_id", form.getBlogId());
        if (!StringUtils.hasText(form.getName())) {
            qw.like("name", form.getName());
        }
        if (form.getStatus() != null) {
            qw.eq("status", form.getStatus());
        }
        if (form.getCommentTimeRange() != null && form.getCommentTimeRange().length > 0) {
            qw.between("comment_time", form.getCommentTimeRange()[0], form.getCommentTimeRange()[1]);
        }

        IPage<Comment> iPage = this.page(pageInfo.setPage(), qw);
        List<CommentDto> list = BeanConvertUtils.coptList(iPage.getRecords(), CommentDto.class);
        return R.success(pageInfo.setPageInfo(iPage.getTotal(), list));
    }

    @Override
    public void updateStatus(int commentId) {
        Comment search = this.getById(commentId);
        if (search == null) {
            throw new BusinessException("编号异常！");
        }
        Comment comment = new Comment();
        comment.setCommentId(commentId);
        comment.setStatus(search.getStatus() == 1 ? 2 : 1);
        this.updateById(comment);
    }


    @Override
    public R getVipCommentPage(int page, int blogId) {
        PageInfo pageInfo = new PageInfo(page, MyConstants.VIP_PAGE_LIMIT);
        IPage<Comment> iPage = this.page(pageInfo.setPage(), new QueryWrapper<Comment>()
                .eq("blog_id", blogId)
                .eq("status", 1)
        );
        List<CommentDto> list = BeanConvertUtils.coptList(iPage.getRecords(), CommentDto.class);
        return R.success(pageInfo.setPageInfo(iPage.getTotal(), list));
    }

    @Override
    public void saveComment(ApiCommentOV form) {
        Blog blog = blogMapper.selectById(form.getBlogId());
        if (blog == null || blog.getBlogStatus() == 2 || blog.getStatus() == 2) {
            throw new BusinessException("博客异常");
        }
        Comment comment = BeanConvertUtils.copyBean(form, Comment.class);
        comment.setCommentTime(new Date());
        this.save(comment);
    }
}




